/**
 * 
 */
package com.yidin.lsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.NearbyFriendsEntry;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 附近用户的功能页面
 * 
 * @author cola
 * 
 */
public class nearBy extends Container {
	private Map<Button, View> group;
	private Button listBtn;
	private Button speedDialBtn;
	private Button mapBtn;
	private Button rightBtn;
	private ViewGroup ViewContainer;
	private ListView dynamicListView;
	private NearbyFriendsAdapter nearbyFriendsAdapter;
	private NearbySpeedDialAdapter nearbySpeedDialAdapter;
	private List<NearbyFriendsEntry> l = null;
	//private List<View> group;

	public nearBy(Activity activity, View contentView,Views father) {
		super(activity, contentView, father);
		// TODO Auto-generated constructor stub
		initialize();
	}

	@Override
	public void checkCache() {
		// TODO Auto-generated method stub

	}

	@Override
	public void findView(View contentView) {
		// TODO Auto-generated method stub
		//inflater = activity.getLayoutInflater();
		View view = null;
//		if (lscManager.getInstance().map == null) {
//			view = inflater.inflate(R.layout.main_map, null);
//			lscManager.getInstance().map = view;
//		} else
//			view = lscManager.getInstance().map;
		
		//group.add(view);

		view = inflater.inflate(R.layout.talk, null);
		ViewContainer.addView(view);
		dynamicListView = (ListView) view
				.findViewById(R.id.myFriendListView);

		this.listBtn = (Button) contentView.findViewById(R.id.list);
		this.speedDialBtn = (Button) contentView.findViewById(R.id.speedDial);
		this.mapBtn = (Button) contentView.findViewById(R.id.map);
		this.rightBtn = (Button) contentView.findViewById(R.id.filter);
		group.put(this.listBtn, view);
		
		Map obj = new HashMap<String, String>();
		obj.put("AccountId", "hlf");

		((LscActivity) activity).showProgress(activity);
		Task task = new Task(Task.TASK_NEARBY_LIST, obj);
		MainService.AddNewTask(task);
	}

	private OnClickListener mapOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			if (lscManager.getInstance().map == null) {
//				View view = inflater.inflate(R.layout.main_map, null);
//				lscManager.getInstance().map = view;
//			}
			
			for (Map.Entry<Button, View> entry : group.entrySet())
				entry.getValue().setVisibility(View.GONE);
			if (group.get(v) == null) {
				//View view = inflater.inflate(R.layout.myphotodetail, null);
				View view = inflater.inflate(R.layout.main_map, null);
				group.put((Button) v, view);
				((LscActivity) activity).setMap();
				ViewContainer.addView(view);
				
				//LoadImageFilter();
			}
			group.get(v).setVisibility(View.VISIBLE);
		}

	};
	
	private GridView myGridView;
	private OnClickListener speedDialBtnOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (group.get(v) == null) {
				group.get(listBtn).setVisibility(View.GONE);
				if (group.get(mapBtn) != null)
					group.get(mapBtn).setVisibility(View.GONE);
				View view = inflater.inflate(R.layout.gridview, null);
				myGridView = (GridView) view.findViewById(R.id.blog);
				myGridView.setOnItemClickListener(gridviewOnItemClickListener);
				group.put((Button) v, view);
				group.get(listBtn).setVisibility(View.GONE);
				ViewContainer.addView(view);
			} else {
				((ViewGroup) group.get(listBtn)).setVisibility(View.GONE);
				if (group.get(mapBtn) != null)
					group.get(mapBtn).setVisibility(View.GONE);
				group.get(speedDialBtn).setVisibility(View.VISIBLE);
			}

			if (l == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", "hlf");

				((LscActivity) activity).showProgress(activity);
				Task task = new Task(Task.TASK_NEARBY_LIST, obj);
				MainService.AddNewTask(task);
			}else{
				if(nearbySpeedDialAdapter == null){
					nearbySpeedDialAdapter = new NearbySpeedDialAdapter(activity,l);
				}
					myGridView.setAdapter(nearbySpeedDialAdapter);
			}
		}

	};
	
	private OnItemClickListener gridviewOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub

				Intent it = new Intent(activity,
						OtherFriendData.class);
				it.putExtra("AccountFrom", l.get(position).AccountId);
				activity.startActivity(it);

			
		}

	};
	
	private OnClickListener listOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (Map.Entry<Button, View> entry : group.entrySet())
				entry.getValue().setVisibility(View.GONE);
			if (group.get(v) == null) {
				View view = inflater.inflate(R.layout.talk, null);
				group.put((Button) v, view);
				ViewContainer.addView(view);
				//LoadImageFilter();
			}
			group.get(v).setVisibility(View.VISIBLE);
			dynamicListView = (ListView) group.get(v)
					.findViewById(R.id.myFriendListView);
			if (l == null) {
			Map obj = new HashMap<String, String>();
			obj.put("AccountId", "hlf");

			((LscActivity) activity).showProgress(activity);
			Task task = new Task(Task.TASK_NEARBY_LIST, obj);
			MainService.AddNewTask(task);
		}else
			dynamicListView.setAdapter(nearbyFriendsAdapter);
		}

	};
	
	@Override
	public void showFunctionsListView(View contentView) {
		// TODO Auto-generated method stub
		this.group = new HashMap<Button, View>();
		ViewContainer = (ViewGroup) contentView
				.findViewById(R.id.ViewContainer);
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		this.rightBtn.setOnClickListener(filterOnClickListener);
		this.listBtn.setOnClickListener(listOnClickListener);
		this.speedDialBtn.setOnClickListener(speedDialBtnOnClickListener);
		this.mapBtn.setOnClickListener(mapOnClickListener);
	}
	
	private String[] sex = new String[] { "不限","男", "女" };
	private boolean select = false;
	private OnClickListener filterOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (select == false) {
				View view = inflater.inflate(R.layout.nearbyfilter, null);
				ViewContainer.getChildAt(0).setVisibility(View.GONE);
				if (ViewContainer.getChildCount() > 1)
					ViewContainer.removeViewAt(1);
				ViewContainer.addView(view);

				Spinner sexSpinner = (Spinner) activity
						.findViewById(R.id.spinnerSex);
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(
						activity, android.R.layout.simple_spinner_item, sex);
				// 设置下拉列表的风格
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				sexSpinner.setAdapter(adapter);
				sexSpinner.setOnItemSelectedListener(itemSelect);
				select = true;
			}else{
				ViewContainer.getChildAt(1).setVisibility(View.GONE);
				ViewContainer.getChildAt(0).setVisibility(View.VISIBLE);
				select = false;
			}
		}

	};
	
	private OnItemSelectedListener itemSelect = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
//			if (parent.getItemAtPosition(position).toString().equals("男"))
//				userSex = "1";
//			else
//				userSex = "0";
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub

		}
	};
	
	@Override
	public void removeView() {
		// TODO Auto-generated method stub
		ViewContainer.removeAllViews();
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_NEARBY_LIST:
			l = (List<NearbyFriendsEntry>) param[1];
			if(l == null) return;
			nearbyFriendsAdapter = new NearbyFriendsAdapter( l,activity);
			dynamicListView.setAdapter(nearbyFriendsAdapter);
			dynamicListView.setOnItemClickListener(listviewOnItemClickListener);
			break;
		}
	}

	private OnItemClickListener listviewOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, OtherFriendData.class);
			it.putExtra("AccountFrom", l.get(position).AccountId);
			activity.startActivity(it);
		}

	};
}
